version 12
clear
capture log close
log using ReplicationFilePRQ.log, replace
#delimit ;
set more off ;
/*
*     ***************************************************************** 
  File-Name:   	ReplicationFile.do	
  Date:        	March 16, 2018
  Purpose:  	Run all models in tables in Boydstun, Highton, and Linn, PRQ
				Assessing the Relationship etween Economic News Coverage 
				and Mass Economic Attitudes
				Produce all figures in paper
  Data Input:	EconomicAttitudes	
  Data Output:	NONE  
*     **************************************************************** */ 

use "~/Dropbox/Economy and Media/Runs/ReplicationFiles/PRQReplicationData.dta";
tsset mdate;
format mdate %tm;
sort mdate;

sort mdate;

drop if year<1978;

*********************************************************;
*********************************************************;
* Generate annual, monthly, quarterly change rates		*;
*********************************************************;
*********************************************************;

generate Aunrate_growth = (unrate-L12.unrate)/L12.unrate*100;
generate Munrate_growth = (unrate-L.unrate)/L.unrate*100;
generate Qunrate_growth = (unrate-L3.unrate)/L3.unrate*100;

generate Munrate_change = d.unrate;
generate Qunrate_change = unrate-L3.unrate;

generate Ainflation = (cpiaucsl-L12.cpiaucsl)/L12.cpiaucsl*100;
generate Minflation = (cpiaucsl-L.cpiaucs)/L.cpiaucs*100;
generate Qinflation = (cpiaucsl-L3.cpiaucs)/L3.cpiaucs*100;

generate Acei = (cei-L12.cei)/L12.cei*100;
generate Mcei = (cei-L.cei)/L.cei*100;
generate Qcei = (cei-L3.cei)/L3.cei*100;

generate Alagei = (lagei-L12.lagei)/L12.lagei*100;
generate Mlagei = (lagei-L.lagei)/L.lagei*100;
generate Qlagei = (lagei-L3.lagei)/L3.lagei*100;

generate ASP = (real_sp_index-L12.real_sp_index)/L12.real_sp_index*100;
generate MSP = (real_sp_index-L.real_sp_index)/L.real_sp_index*100;
generate QSP = (real_sp_index-L3.real_sp_index)/L3.real_sp_index*100;


generate Ardipc = (rdpi_pc-L12.rdpi_pc)/L12.rdpi_pc*100;
generate Mrdipc = (rdpi_pc-L.rdpi_pc)/L.rdpi_pc*100;
generate Qrdipc = (rdpi_pc-L3.rdpi_pc)/L3.rdpi_pc*100;


*********************************************************;
*********************************************************;
* Generate election and events variables				*;
*********************************************************;
*********************************************************;
generate election = 0;
replace election=10 if ( (year==1980 | year==1984 | year==1988 | 
	year==1992 | year==1996	| year==2000 | year==2004 | 
	year==2008 | year==2012) & (month==11) );
replace election=9 if ( (year==1980 | year==1984 | year==1988 | 
	year==1992 | year==1996	| year==2000 | year==2004 | 
	year==2008 | year==2012) & (month==10 | month ==12) );
replace election=8 if ( (year==1980 | year==1984 | year==1988 | 
	year==1992 | year==1996	| year==2000 | year==2004 | 
	year==2008 | year==2012) & (month==9) );
replace election=8 if ( (year==1981 | year==1985 | year==1989 | 
	year==1993 | year==1997	| year==2001 | year==2005 | 
	year==2009 | year==2013) & (month ==1) );
replace election=7 if ( (year==1980 | year==1984 | year==1988 | 
	year==1992 | year==1996	| year==2000 | year==2004 | 
	year==2008 | year==2012) & (month==8) );
replace election=7 if ( (year==1981 | year==1985 | year==1989 | 
	year==1993 | year==1997	| year==2001 | year==2005 | 
	year==2009 | year==2013) & (month ==2) );
	
replace election=6 if ( (year==1980 | year==1984 | year==1988 | 
	year==1992 | year==1996	| year==2000 | year==2004 | 
	year==2008 | year==2012) & (month==7) );
replace election=6 if ( (year==1981 | year==1985 | year==1989 | 
	year==1993 | year==1997	| year==2001 | year==2005 | 
	year==2009 | year==2013) & (month ==3) );
	
replace election=5 if ( (year==1980 | year==1984 | year==1988 | 
	year==1992 | year==1996	| year==2000 | year==2004 | 
	year==2008 | year==2012) & (month==6) );
replace election=5 if ( (year==1981 | year==1985 | year==1989 | 
	year==1993 | year==1997	| year==2001 | year==2005 | 
	year==2009 | year==2013) & (month ==4) );
	
replace election=4 if ( (year==1980 | year==1984 | year==1988 | 
	year==1992 | year==1996	| year==2000 | year==2004 | 
	year==2008 | year==2012) & (month==5) );
replace election=4 if ( (year==1981 | year==1985 | year==1989 | 
	year==1993 | year==1997	| year==2001 | year==2005 | 
	year==2009 | year==2013) & (month ==5) );
	
replace election=3 if ( (year==1980 | year==1984 | year==1988 | 
	year==1992 | year==1996	| year==2000 | year==2004 | 
	year==2008 | year==2012) & (month==4) );
replace election=3 if ( (year==1981 | year==1985 | year==1989 | 
	year==1993 | year==1997	| year==2001 | year==2005 | 
	year==2009 | year==2013) & (month ==6) );
	
replace election=2 if ( (year==1980 | year==1984 | year==1988 | 
	year==1992 | year==1996	| year==2000 | year==2004 | 
	year==2008 | year==2012) & (month==3) );
replace election=2 if ( (year==1981 | year==1985 | year==1989 | 
	year==1993 | year==1997	| year==2001 | year==2005 | 
	year==2009 | year==2013) & (month ==7) );
	
replace election=1 if ( (year==1980 | year==1984 | year==1988 | 
	year==1992 | year==1996	| year==2000 | year==2004 | 
	year==2008 | year==2012) & (month==2) );
replace election=1 if ( (year==1981 | year==1985 | year==1989 | 
	year==1993 | year==1997	| year==2001 | year==2005 | 
	year==2009 | year==2013) & (month ==8) );

	
generate electionQ=0;
replace electionQ=1 if( (year==1980 | year==1984 | year==1988 | 
	year==1992 | year==1996	| year==2000 | year==2004 | 
	year==2008 | year==2012) & (month==10 | month==11 | month==12) );
	
generate events = 0;
replace events = 1 if (year==1981 & month==4); *Reagan attempted ass March 30, 1981;
replace events = 1 if (year==1983 & month==11); *Granada invasion Oct 25, 1983;
replace events = -1 if (year==1986 & month==11); *Iran Contra scandal exposed by leak;
replace events = 1 if (year==1989 & month==12); *Fall of Berlin Wall;
replace events = -1 if (year==1990 & month>=8); *Iraq invades Kuwait;
replace events = 1 if (year==1991 & (month>=1 & month<=3)); *Operation Desert Storm (17 January 1991 – 28 February 1991);
replace events = -1 if (year==2000 & month==9);* 9/11 terrorist attacks;
replace events = 1 if (year==2003 & (month>=4 & month<=5)); *20 March to 1 May 2003 and signalled the start of the Iraq War, which the United States dubbed Operation Iraqi Freedom;
replace events = -1 if (year==2005 & month==9); *month after katrina*;
replace events = 1 if (year==2007 & month==1); *killed Saddam Dec 30, 2006*;
replace events = 1 if (year==2011 & month==5); *killed Osama bin Laden May 2, 2011; 
replace events = -1 if (year==2008 & month==9); *September 15, 2008: Lehman Brothers Bankruptcy Triggered Global Panic;
replace events = 1 if (year==2011 & month==5); *killed Osama bin Laden May 2, 2011; 


generate DemPres=0;
replace DemPres=1 if (year>=1993 & year<=2000);
replace DemPres=1 if (year>=2009);

generate Reagan=0;
replace Reagan=1 if (year>=1981 & year<=1988);
generate Bush1=0;
replace Bush1=1 if (year>=1989 & year<=1992);
generate Clinton=0;
replace Clinton=1 if (year>=1993 & year<=2000);
generate Bush2=0;
replace Bush2=1 if (year>=2001 & year<=2008);
generate Obama=0;
replace Obama=1 if (year>=2009);



*********************************************************;
*********************************************************;
* 1. Estimate Saturated ICS model used for block F tests*;
* Column 1 of Table 1									*;
*********************************************************;
*********************************************************;

regress ics   					
	L(0/2).Minflation   L(0/2).Qinflation  				
	L(0/2).Mrdipc   L(0/2).Qrdipc   				
	L(0/2).Mjobs     L(0/2).Qjobs         				
	L(0/2).unrate L12.unrate													
	L(0/2).MSP   L(0/2).QSP    
	L(0/2).Mlagei L(0/2).Qlagei
	L(0/2).Mcei L(0/2).Qcei 
	L(0/2).Ainflation L(0/2).Ardipc L(0/2).Ajobs
	L(0/2).ASP L(0/2).Alagei L(0/2).Acei 
	  if tin(1980m1,2014m12); 
 
*********************************************************;
*********************************************************;
* 2. Block F tests for economic measures 				*;
* Column 1 of Table 1									*;
*********************************************************;
*********************************************************;

test Minflation=L1.Minflation=L2.Minflation=Qinflation=L1.Qinflation=L2.Qinflation=
	Ainflation=L1.Ainflation=L2.Ainflation=0;
test Mrdipc=L1.Mrdipc=L2.Mrdipc=Qrdipc=L1.Qrdipc=L2.Qrdipc=Ardipc=L.Ardipc=L2.Ardipc=0;
test Mjobs=L1.Mjobs=L2.Mjobs=Qjobs=L1.Qjobs=L2.Qjobs=Ajobs=L.Ajobs=L2.Ajobs=0;
test unrate=L1.unrate=L2.unrate=L12.unrate=0;
test MSP=L1.MSP=L2.MSP=QSP=L1.QSP=L2.QSP=ASP=L.ASP=L2.ASP=0;
test Mlagei=L1.Mlagei=L2.Mlagei=Qlagei=L1.Qlagei=L2.Qlagei=Alagei=L.Alagei=L2.Alagei=0;
test Mcei=L1.Mcei=L2.Mcei=Qcei=L1.Qcei=L2.Qcei=Acei=L.Acei=L2.Acei=0;

**** Save residuals and predicted values from the model ***;
predict icsr, resid;
predict icsp, xb;

*********************************************************;
*********************************************************;
* 3. Estimate SAME Saturated model of tone used for 	*;
* block F tests in Column 2 of Table 2					*;
*********************************************************;
*********************************************************;

regress tone_combined   					
	L(0/2).Minflation   L(0/2).Qinflation  				
	L(0/2).Mrdipc   L(0/2).Qrdipc   				
	L(0/2).Mjobs     L(0/2).Qjobs         				
	L(0/2).unrate L12.unrate													
	L(0/2).MSP   L(0/2).QSP    
	L(0/2).Mlagei L(0/2).Qlagei
	L(0/2).Mcei L(0/2).Qcei 
	L(0/2).Ainflation L(0/2).Ardipc L(0/2).Ajobs
	L(0/2).ASP L(0/2).Alagei L(0/2).Acei 
	  if tin(1980m1,2014m12); 
 
*********************************************************;
*********************************************************;
* 4. Block F tests for economic measures 				*;
* Column 2 of Table 1									*;
*********************************************************;
*********************************************************;

test Minflation=L1.Minflation=L2.Minflation=Qinflation=L1.Qinflation=L2.Qinflation=
	Ainflation=L1.Ainflation=L2.Ainflation=0;
test Mrdipc=L1.Mrdipc=L2.Mrdipc=Qrdipc=L1.Qrdipc=L2.Qrdipc=Ardipc=L.Ardipc=L2.Ardipc=0;
test Mjobs=L1.Mjobs=L2.Mjobs=Qjobs=L1.Qjobs=L2.Qjobs=Ajobs=L.Ajobs=L2.Ajobs=0;
test unrate=L1.unrate=L2.unrate=L12.unrate=0;
test MSP=L1.MSP=L2.MSP=QSP=L1.QSP=L2.QSP=ASP=L.ASP=L2.ASP=0;
test Mlagei=L1.Mlagei=L2.Mlagei=Qlagei=L1.Qlagei=L2.Qlagei=Alagei=L.Alagei=L2.Alagei=0;
test Mcei=L1.Mcei=L2.Mcei=Qcei=L1.Qcei=L2.Qcei=Acei=L.Acei=L2.Acei=0;

**** Save residuals and predicted values from the model ***;
predict toner, resid;
predict tonep, xb;


*********************************************************;
*********************************************************;
* 5. Estimate regression of ics residuals on tone 		*;
*	residuals											*;
* Column 1 of Table 2									*;
*********************************************************;
*********************************************************;

regress icsr L1.icsr L6.icsr L12.icsr 
	toner if tin(1980m1,2014m12);
estat ic;

**** Test for autocorrelation in the residuals ***;
actest, robust lags(12);
**** Save residuals from the model ***;
predict satres, resid;


*********************************************************;
*********************************************************;
* 6. Estimate model of RAW ics as function of RAW tone 	*;
*	and all economic variables in saturated models		*;
* Column 2 of Table 2									*;
*********************************************************;
*********************************************************;

regress ics   					
	L.ics L6.ics L12.ics tone_combined 
	L(0/2).Minflation   L(0/2).Qinflation  				
	L(0/2).Mrdipc   L(0/2).Qrdipc   				
	L(0/2).Mjobs     L(0/2).Qjobs         				
	L(0/2).unrate L12.unrate													
	L(0/2).MSP   L(0/2).QSP    
	L(0/2).Mlagei L(0/2).Qlagei
	L(0/2).Mcei L(0/2).Qcei 
	L(0/2).Ainflation L(0/2).Ardipc L(0/2).Ajobs
	L(0/2).ASP L(0/2).Alagei L(0/2).Acei 
	  if tin(1980m1,2014m12); 
 
test Minflation=L1.Minflation=L2.Minflation=Qinflation=L1.Qinflation=L2.Qinflation=
	Ainflation=L1.Ainflation=L2.Ainflation=0;
test Mrdipc=L1.Mrdipc=L2.Mrdipc=Qrdipc=L1.Qrdipc=L2.Qrdipc=Ardipc=L.Ardipc=L2.Ardipc=0;
test Mjobs=L1.Mjobs=L2.Mjobs=Qjobs=L1.Qjobs=L2.Qjobs=Ajobs=L.Ajobs=L2.Ajobs=0;
test unrate=L1.unrate=L2.unrate=L12.unrate=0;
test MSP=L1.MSP=L2.MSP=QSP=L1.QSP=L2.QSP=ASP=L.ASP=L2.ASP=0;
test Mlagei=L1.Mlagei=L2.Mlagei=Qlagei=L1.Qlagei=L2.Qlagei=Alagei=L.Alagei=L2.Alagei=0;
test Mcei=L1.Mcei=L2.Mcei=Qcei=L1.Qcei=L2.Qcei=Acei=L.Acei=L2.Acei=0;

actest, robust lags(12);
predict icsres, resid;

*********************************************************;
*********************************************************;
* 7. Graphs in Figures, tone_combined, 2 residual series*;
*********************************************************;
*********************************************************;
twoway (tsline tone_combined if tin(1980m1,2014m12), 
	recast(line)), title() 
	ttitle() subtitle() 
 	ytitle("Average Probability an Article is Positive") xtitle(Date)
	graphregion(fcolor(white) lcolor(black))
	name(Graph_Tone_Combined, replace);
graph save "Graphs/Graph_Tone_Combined.gph", replace;

twoway 
	(line icsr mdate, lcolor(black) lwidth(thin))
	, ytitle(ICS residuals) ytitle(, color(black) margin(medsmall)) 
	yline(0, lwidth(vthin) lpattern(solid) lcolor(black)) 
	ylabel(, labsize(vsmall) angle(horizontal) nogrid) xtitle("") 
	xlabel(, labsize(vsmall)) graphregion(fcolor(white) lcolor(black));
graph save Graph "Graphs/Graph_figure2a.gph" ,replace;

twoway 
	(line toner mdate, lcolor(black) lwidth(thin))
	, ytitle(Media tone residuals) ytitle(, color(black) margin(medsmall)) 
	yline(0, lwidth(vthin) lpattern(solid) lcolor(black)) 
	ylabel(, labsize(vsmall) angle(horizontal) nogrid) xtitle("") 
	xlabel(, labsize(vsmall)) graphregion(fcolor(white) lcolor(black));
graph save Graph "Graphs/Graph_figure2b.gph" ,replace;

graph combine "Graphs/Graph_figure2a.gph" "Graphs/Graph_figure2b.gph"
	, cols(1) xsize(5) ysize(5) graphregion(fcolor(white) lcolor(black));
graph save Graph "Graphs/Graph_figure2.gph" ,replace;


*********************************************************;
*********************************************************;
* 8. Descriptive Statistics, Appendix					*;
*********************************************************;
*********************************************************;

summarize ics tone_combined icsr toner if tin(1980m1,2014m12);


*********************************************************;
*********************************************************;
* 9. Saturate ICS Lags 0 and 1 only 					*;
* Appendix Table A2, Row 2								*;
*********************************************************;
*********************************************************;

regress ics   					
	L(0/1).Minflation   L(0/1).Qinflation  				
	L(0/1).Mrdipc   L(0/1).Qrdipc   				
	L(0/1).Mjobs     L(0/1).Qjobs         				
	L(0/1).unrate L12.unrate													
	L(0/1).MSP   L(0/1).QSP    
	L(0/1).Mlagei L(0/1).Qlagei
	L(0/1).Mcei L(0/1).Qcei 
	L(0/1).Ainflation L(0/1).Ardipc L(0/1).Ajobs
	L(0/1).ASP L(0/1).Alagei L(0/1).Acei 
	  if tin(1980m1,2014m12); 
estat ic;
predict icssat1_annual_lag2, resid;
predict icssat1hat_annual_lag2, xb;


regress tone_combined   					
	L(0/1).Minflation   L(0/1).Qinflation  				
	L(0/1).Mrdipc   L(0/1).Qrdipc   				
	L(0/1).Mjobs     L(0/1).Qjobs         				
	L(0/1).unrate L12.unrate													
	L(0/1).MSP   L(0/1).QSP    
	L(0/1).Mlagei L(0/1).Qlagei
	L(0/1).Mcei L(0/1).Qcei 
	L(0/1).Ainflation L(0/1).Ardipc L(0/1).Ajobs
	L(0/1).ASP L(0/1).Alagei L(0/1).Acei 
	  if tin(1980m1,2014m12); 
estat ic;
predict tonesat1_annual_lag2, resid;
predict tonesat1hat_annual_lag2, xb;

regress icssat1_annual_lag2 L.icssat1_annual_lag2 L6.icssat1_annual_lag2 L12.icssat1_annual_lag2 
	tonesat1_annual_lag2 if tin(1980m1,2014m12);
actest, robust lags(12);
predict satres_lag2, resid;

*********************************************************;
*********************************************************;
* 10. Saturate ICS Lags 0,1,2,3							*;
* Appendix Table A2, Row 3								*;
*********************************************************;
*********************************************************;

regress ics   					
	L(0/3).Minflation   L(0/3).Qinflation  				
	L(0/3).Mrdipc   L(0/3).Qrdipc   				
	L(0/3).Mjobs     L(0/3).Qjobs         				
	L(0/3).unrate L12.unrate													
	L(0/3).MSP   L(0/3).QSP    
	L(0/3).Mlagei L(0/3).Qlagei
	L(0/3).Mcei L(0/3).Qcei 
	L(0/3).Ainflation L(0/3).Ardipc L(0/3).Ajobs
	L(0/3).ASP L(0/3).Alagei L(0/3).Acei 
	  if tin(1980m1,2014m12); 
estat ic;
predict icssat1_annual_lag3, resid;
predict icssat1hat_annual_lag3, xb;


regress tone_combined   					
	L(0/3).Minflation   L(0/3).Qinflation  				
	L(0/3).Mrdipc   L(0/3).Qrdipc   				
	L(0/3).Mjobs     L(0/3).Qjobs         				
	L(0/3).unrate L12.unrate													
	L(0/3).MSP   L(0/3).QSP    
	L(0/3).Mlagei L(0/3).Qlagei
	L(0/3).Mcei L(0/3).Qcei 
	L(0/3).Ainflation L(0/3).Ardipc L(0/3).Ajobs
	L(0/3).ASP L(0/3).Alagei L(0/3).Acei 
	  if tin(1980m1,2014m12); 
estat ic;
predict tonesat1_annual_lag3, resid;
predict tonesat1hat_annual_lag3, xb;

regress icssat1_annual_lag3 L.icssat1_annual_lag3 L12.icssat1_annual_lag3 
	tonesat1_annual_lag3 if tin(1980m1,2014m12);
actest, robust lags(12);
predict satres_lag3, resid;


*********************************************************;
*********************************************************;
* 11. Add events to final model of purged ICS			*;
* Table A3												*;
*********************************************************;
*********************************************************;
regress icsr L.icsr L6.icsr 
	toner 
	events election electionQ DemPres
	if tin(1980m1,2014m12);
estat ic;
actest, robust lags(12);
predict satres_event3, resid;

* separate admin dummies*;
regress icsr L.icsr L6.icsr   
	toner 
	events election electionQ Reagan Bush1 Clinton Bush2 Obama
	if tin(1980m1,2014m12);
estat ic;
actest, robust lags(12);
predict satres_event4, resid;


*********************************************************;
*********************************************************;
* 12. Graphs in Appendix Figures, predict and actual	*;
*********************************************************;

twoway 
	(line tone_combine tonep mdate, lcolor(black) lwidth(thin)) 
	if tin(1980m1,2014m12), ytitle(ICS residuals) 
	ytitle(, color(black) margin(medsmall)) 
	yline(0, lwidth(vthin) lpattern(solid) lcolor(black)) 
	ylabel(, labsize(vsmall) angle(horizontal) nogrid) xtitle("") 
	xlabel(, labsize(vsmall)) graphregion(fcolor(white) lcolor(black));
graph save Graph "Graphs/GraphA1.gph" ,replace;

twoway 
	(line ics icsp mdate, lcolor(black) lwidth(thin)) 
	if tin(1980m1,2014m12), ytitle(ICS residuals) 
	ytitle(, color(black) margin(medsmall)) 
	yline(0, lwidth(vthin) lpattern(solid) lcolor(black)) 
	ylabel(, labsize(vsmall) angle(horizontal) nogrid) xtitle("") 
	xlabel(, labsize(vsmall)) graphregion(fcolor(white) lcolor(black));
graph save Graph "Graphs/GraphA2.gph" ,replace;



**************************************************************;
* 13. Create small dataset for R								 *;
**************************************************************;

keep icsr toner;
saveold "~/Dropbox/Economy and Media/Runs/ReplicationFiles/PRQ_SVAR_Data.dta", version(12) replace;


exit;
